package com.topsmob.piepline;

import com.topsmob.tool.HttpClientUtils;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * com.topsmob
 * Description:
 * User:李耀华
 * Date:2017-06-19.10:36
 */
public class DeCmsPiePline implements Pipeline {
    private  Spider spider;
    private  boolean isFirst = true;
    private String lastId =  null;
    Properties properties = new Properties();
    public DeCmsPiePline(Spider spider){
        this.spider = spider;
        this.isFirst = true;
        try {
            InputStream inputStream = new FileInputStream(getClass().getResource("/").getFile().toString()+"res.properties");
            properties.load(inputStream);
            inputStream.close();
            lastId = properties.getProperty("lastId");
            System.out.println("最后一次:"+lastId);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void process(ResultItems resultItems, Task task) {
        //最后一条的话 停止爬虫
        if(resultItems.get("postId").toString().equals(lastId)){
            spider.stop();
            System.out.println("存在记录停止爬虫");
        }
        Map<String,String> map = new HashMap();
        map.put("userid","admin");
        map.put("pwd","admin");
        map.put("typeid","1");
        map.put("title",resultItems.get("title").toString());
        map.put("body",resultItems.get("content").toString());
        map.put("ishtml","1");
        map.put("remote","1");
        map.put("autolitpic","1");
        List<String> tagList = resultItems.get("label");
        String tags = "";
        for(String str:tagList){
            tags += str+",";
        }
        map.put("tags",tags);
        String str = HttpClientUtils.doPost("http://www.cms.com/dede/etpost.php",map,"utf-8");
        if(isFirst && str.equals("[ok]")){
            //写Properties文件

            try {
                FileOutputStream oFile = new FileOutputStream(getClass().getResource("/").getFile().toString()+"res.properties", false);
                properties.put("lastId",resultItems.get("postId").toString());
                properties.store(oFile,"修改最后一条记录的Id");
                oFile.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            isFirst = false;
        }

        System.out.println(str);
    }
}
